<?php
//(c) Copyright 2016 wonyun. All Rights Reserved. 
/**
 *
 * 订单相关模块
 * author：Rogermmg
 **/ 
class OrderAction extends Action{
    public function _initialize(){
		$action = array(
			'users'=>array('index', 'add','search','lists', 'edit', 'del'),
			'anonymous'=>array('')
		);
		B('Authenticate', $action);
	}
	
	public function index(){
		$this->alert = parseAlert();
		$this->display();
	}
	
	//订单添加
	public function add(){
		if($this->isPost()){
			$data['type']=$_POST['type'];
			$data['delivery']=$_POST['executor_id'];
			$data['remark']=$_POST['remark'];
			$data['user_id']=$_SESSION['user_id'];
			$data['status']=1;
			$data['creattime']=strtotime($_POST['creattime']);
			if($data['type'] == '' or $data['creattime'] == ''){
				alert('error','请填写必填项再提交',U('chrm/order/add'));
			}
			if(D('Order')->addOrder($data)){
				alert('success','保存成功',U('chrm/order/add'));
			}else{
				alert('error','保存失败',U('chrm/order/add'));
			}
		}else{
			$this->alert = parseAlert();
			$this->display();
		}
		
	}
	
	//订单搜索
	public function search(){
		$this->alert = parseAlert();
		$this->display();
	}
	
	//订单列表
	public function lists(){
		$p = $this->_get('p','intval',1);
		$type = $this->_get('type','trim','0');
		$status=1;
		$d_order = D('Order');
		$orderpagelist = $d_order->getOrderPageList($p,$status,$type);
		
		$d_user = D('User');
		foreach($orderpagelist['orderlist'] as $key=>$order){
			$user = $d_user->getUserInfo(array('user_id'=>$order["delivery"]));
			$neworder[$key]=$order;
			$neworder[$key]['dname']=$user['name'];
		}
		
//		dump($neworder);exit();
		$this->assign('orderlist', $neworder);
		$this->assign('page', $orderpagelist['page']);
		$this->assign('type_array',array('0'=>'所有单','1'=>'正常单','2'=>'超时单','3'=>'异常单','4'=>'欺诈单','5'=>'赔餐单'));
		$this->assign('type', $type);
		$this->alert = parseAlert();
		$this->display();
	}
	
	//订单编辑
	public function edit(){
		$this->alert = parseAlert();
		$this->display();
	}
	
	//删除订单
	public function del(){
		if ($this->isAjax()){
			$oid=$_POST['oid'];
			$data['update']=time();
			$data['status']=0;
			
			$d_del=D("Order");
			if($d_del->Delete($oid,$data)){
				$injson['info']="成功删除！";
				$this->ajaxReturn($injson);
			}
		}
	}
}